﻿using System;
using System.Windows.Forms;
using Scommesse.Presenters;

namespace Scommesse.View
{
    public partial class Base : Form
    {
        private IBasePresenter _presenter;

        public Base()
        {
            InitializeComponent();
        }

        public void AddListener(IBasePresenter presenter)
        {
            _presenter = presenter;
        }

        private void Base_FormClosed(object sender, FormClosedEventArgs e)
        {
            _presenter.CloseProgram();
        }

        private void Base_Shown(object sender, EventArgs e)
        {
            _presenter.PopulateTreeViewAllEvents();
            _presenter.PopulateCategorieMenuStrip();
            _presenter.SetCredito();
            _presenter.RefreshImporto();
        }

        private void EventiThreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode selectedNode = eventiThreeView.SelectedNode;
            _presenter.SelezionaPuntata(selectedNode);
            _presenter.RefreshMoltiplicatoreComplessivo();
            _presenter.RefreshVincitaPotenziale();
        }

        private void CarrelloListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var selectedItem = (String)carrelloListBox.SelectedItem;
            // try-catch in caso si clicchi una linea vuota
            try
            {
                _presenter.RemovePuntata(selectedItem);
                _presenter.RefreshMoltiplicatoreComplessivo();
                _presenter.RefreshVincitaPotenziale();
            }
            catch
            {
                
            }
        }

        public void categoriaItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            _presenter.PopulateTreeViewByMenuStrip((ToolStripMenuItem) e.ClickedItem);
        }

        private void Calendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            _presenter.PopolateEventiList();
        }

        private void PuntaOraButton_Click(object sender, EventArgs e)
        {
            _presenter.PopulateTreeViewNextEvents();
        }

        private void confermaButton_Click(object sender, EventArgs e)
        {
            _presenter.ConfermaScommessa();
        }

        private void LeTueScommesseButton_Click(object sender, EventArgs e)
        {
            _presenter.LeTueScommesse();
        }

        private void CategorieMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            _presenter.PopulateTreeViewByCategoria(e.ClickedItem.Text);
        }

        private void RicaricaBetCardButton_Click(object sender, EventArgs e)
        {
            _presenter.RicaricaBetCard();
        }

        private void Base_Activated(object sender, EventArgs e)
        {
            _presenter.SetCredito();
        }

        private void RegolamentoButtonClick(object sender, EventArgs e)
        {
            new RegolamentoPresenter(new RegolamentoDialog());
        }

        public DateTime CalendarDate
        {
            get { return calendar.SelectionStart; }
            set { calendar.SelectionStart = value; }
        }

        public TreeView EventiTreeView
        {
            get { return eventiThreeView; }
            set { eventiThreeView = value; }
        }

        public MenuStrip CategoriaMenuStrip
        {
            get { return categorieMenuStrip; }
            set { categorieMenuStrip = value; }
        }

        public ListBox CarrelloListBox
        {
            get { return carrelloListBox; }
            set { carrelloListBox = value; }
        }

        public String CreditoEffettivo
        {
            get { return creditoEffettivoLabel.Text; }
            set { creditoEffettivoLabel.Text = value; }
        }

        public String Importo
        {
            get { return importoTextBox.Text ; }
            set { importoTextBox.Text = value; }
        }

        public String MoltiplicatoreValue
        {
            get { return labelMoltiplicatoreValue.Text; }
            set { labelMoltiplicatoreValue.Text = value; }
        }
        
        public String VincitaPotenziale
        {
            get { return labelVincitaPotenzialeValue.Text; }
            set { labelVincitaPotenzialeValue.Text = value; }
        }

        public String ImportoError
        {
            set { errorProvider1.SetError(importoTextBox, value); }
        }

        #region ImportoTextBox Controls
        private void importoTextBox_TextChanged(object sender, EventArgs e)
        {
            _presenter.RefreshVincitaPotenziale();
        }

        private void importoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            Utility.NumericTextBox.BlockChars(sender, e);
        }

        private void importoTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                confermaButton.PerformClick();
        }
        #endregion
    }
}